/**
 * 
 */
package edu.umd.clip.lm.model.data;

import java.io.IOException;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class WritableTrainingData extends AbstractTrainingData {
	protected final TrainingDataWriter writer;
	protected boolean started = false;
	
	/**
	 * @param writer
	 */
	public WritableTrainingData(TrainingDataWriter writer) {
		this.writer = writer;
	}

	public void add(TrainingDataBlock block) throws IOException {
		if (!started) {
			start();
		}
		writer.write(this, block);
	}
	
	public void start() {
		if (!started) {
			writer.start(this);
			started = true;
		}
	}
	
	/**
	 * flushes unwritten data, writes the terminator, does *not* close the underlying output stream
	 * @throws IOException
	 */
	public void finish() throws IOException {
		writer.write(this, TrainingDataBlock.TERMINATOR);
		writer.finish(this);
	}
}
